package com.eis.admin.mvc.controller.base;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ui.ModelMap;

import com.eis.admin.mvc.common.utils.MVCConstants;
import com.eis.persist.model.user.PersistUser;

public class MVCBaseController {
	protected String appPrefix = "";
	protected int tabId;
	protected JdbcTemplate functionDataSource;
	
	protected PersistUser getCurrentUser(HttpServletRequest request) {
		PersistUser persistUser = (PersistUser) request.getSession().getAttribute(MVCConstants.USER_SESSION_KEY);
		
		return persistUser;
	}
	
	public void setAppPrefix(String appPrefix) {
		this.appPrefix = appPrefix;
	}

	public int getTabId() {
		return tabId;
	}

	public void setTabId(int tabId) {
		this.tabId = tabId;
	}
	
	public void setWarningMessage(HttpServletRequest request, String message) {
		List<String> warnMessages = new ArrayList<String>();
		warnMessages.add(message);
		setWarningMessages(request, warnMessages);
	}
	
	protected ModelMap prepareRedirectWarnMessage(String message) {
		ModelMap mmap = new ModelMap();
		mmap.addAttribute(MVCConstants.WARN_MESSAGE_KEY, message);
		return mmap;
	}
	
	protected ModelMap prepareRedirectErrorMessage(String message) {
		ModelMap mmap = new ModelMap();
		mmap.addAttribute(MVCConstants.ERROR_MESSAGE_KEY, message);
		return mmap;
	}

	public void setWarningMessages(HttpServletRequest request, List<String> messages) {
		request.setAttribute(MVCConstants.WARN_MESSAGE_KEY, messages);
	}
	
	public void setWarningMessages(HttpServletRequest request, String message) {
		List<String> messages = new ArrayList<String>();
		messages.add(message);
		request.setAttribute(MVCConstants.WARN_MESSAGE_KEY, messages);
	}

	public void setErrorMessage(HttpServletRequest request, String message) {
		List<String> errorMessages = new ArrayList<String>();
		errorMessages.add(message);
		setErrorMessages(request, errorMessages);
	}

	public void setErrorMessages(HttpServletRequest request, List<String> messages) {
		request.setAttribute(MVCConstants.ERROR_MESSAGE_KEY, messages);
	}
	
	public void setFunctionDataSource(JdbcTemplate functionDataSource) {
		this.functionDataSource = functionDataSource;
	}
}
